package com.limh.readers.fragment

import android.os.Bundle
import android.support.v4.view.ViewPager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.HorizontalScrollView
import android.widget.RadioButton
import android.widget.RadioGroup
import butterknife.BindView
import com.limh.readers.R
import com.limh.readers.adapter.FragmentAdapter

/**
 * Function:历史
 * author: limh
 * time:2017/7/3
 */
class PastFragment : BaseFragment() {
    @BindView(R.id.vp_past)
    lateinit var vpPast: ViewPager
    @BindView(R.id.radio_menu)
    lateinit var radioMenu: RadioGroup
    @BindView(R.id.hor_scroll)
    lateinit var scroll: HorizontalScrollView

    private lateinit var fragments: ArrayList<BaseFragment>
    private val titles = arrayOf("读者", "意林", "意林少年版", "意林原创版", "故事会", "轻小说", "故事林")
    private val ids = arrayOf(R.id.radio_0, R.id.radio_1, R.id.radio_2, R.id.radio_3, R.id.radio_4, R.id.radio_5, R.id.radio_6)

    val TAG = "PastFragment"

    override fun findViews(inflater: LayoutInflater?, container: ViewGroup?): View {
        return inflater?.inflate(R.layout.fragment_past, container, false) as View
    }

    override fun initViews() {
        fragments = ArrayList()
        for (i in titles) {
            val b = Bundle()
            b.putString("title", i)
            val itemFragment = PeriodListFragment()
            itemFragment.arguments = b
            fragments.add(itemFragment)
        }
        vpPast.adapter = FragmentAdapter(childFragmentManager, fragments)
        vpPast.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
                if (state == 2) {
                    var rb: RadioButton? = null
                    radioMenu.check(ids[vpPast.currentItem])
                    rb = activity.findViewById(ids[vpPast.currentItem])
                    val scrollX = scroll.scrollX
                    val left = rb.left
                    val leftScreen = left - scrollX
                    scroll.smoothScrollBy(leftScreen - (width!! / 2) + rb.width / 2, 0)
                }
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
            }
        })
        vpPast.currentItem = 0
        radioMenu.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.radio_0 -> vpPast.setCurrentItem(0, true)
                R.id.radio_1 -> vpPast.setCurrentItem(1, true)
                R.id.radio_2 -> vpPast.setCurrentItem(2, true)
                R.id.radio_3 -> vpPast.setCurrentItem(3, true)
                R.id.radio_4 -> vpPast.setCurrentItem(4, true)
                R.id.radio_5 -> vpPast.setCurrentItem(5, true)
                R.id.radio_6 -> vpPast.setCurrentItem(6, true)
            }
        }
    }

    override fun initDatas() {

    }
}